package guowei.guliproduct.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.guliproduct.product.dao.SpuImagesDao;
import guowei.guliproduct.product.entity.SpuImagesEntity;
import guowei.guliproduct.product.service.SpuImagesService;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImagesEntity> page = this.page(
                new Query<SpuImagesEntity>().getPage(params),
                new QueryWrapper<SpuImagesEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 从商品上传得到图片url路径 ，从而进行保存
     * @param images
     */
    @Override
    public void saveAllImages(Long spuId,List<String> images) {
        ArrayList<SpuImagesEntity> list = new ArrayList<>();
        for (String imgUrl:images) {
            if (imgUrl != null) {
                SpuImagesEntity entity = new SpuImagesEntity();
                entity.setSpuId(spuId);
                entity.setImgUrl(imgUrl);
                list.add(entity);
            }
        }
        this.saveBatch(list);
    }

}